<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/4/24 0024
 * Time: 下午 2:26
 */
namespace app\common\model;

use app\admin\controller\Base;
use think\Db;
use think\Model;

class Shoppingcar extends BaseModel{

    const STATUS_VALID = 1;  //状态  有效
    const STATUS_DELETE = 2; //状态  删除
    const STAUTS_LOWER_FRAME = 3; //状态下架


    /**
     * @return $this
     */

    public function Productspecgroups(){
            return $this->hasOne('productspecgroups','id','specId');
    }

    /**
     * @return \think\model\relation\HasOne
     * 获取商品店铺关联
     */
    public function Product()
    {
        return $this->hasOne('product','id','productId');
    }



    /**
     * 获取当前商品在购物车内是否存在
     */
    public function getCurrentShoppingcar($productId,$userId){
      return self::where([
            'productId'=>$productId,
            'userId'=>$userId,
        ])->find();

    }

    public function getCurrentShoppingcarSpec($productId,$userId,$specId){
        return self::where([
            'productId'=>$productId,
            'userId'=>$userId,
            'specId'=>$specId
        ])->find();

    }

    /**
     * 购物车商品删除
     * @param $productId
     * @param $userId
     * @return int
     */

    public function delCurrentShoppingCar($productId,$userId){
        return self::where([
            'productId'=>$productId,
            'userId'=>$userId,
            'status'=>   self::STATUS_DELETE
        ])->delete();
    }

    /**
     *更新用户购物车
     */
    public function updateMyShoppingCar($data){
        return self::where(['id'=>$data['id']])->update($data);
    }


    /**
     * 添加商品购物车
     */

    public function addMyShoppingCar($data){
       return self::save($data);
      // echo $this->getLastSql();exit;
        //return
    }

    /**
     * 但删除
     * @param $productId
     * @param $userId
     * @return $this
     */
    public function delProduct($shoppingCarIds,$userId){
        return $this->where('id','=',$shoppingCarIds)
            ->where('userId','=',$userId)
            ->update(array('status'=>self::STATUS_DELETE));
    }

    /**
     * 批量删除
     * @param $productIds
     * @param $userId
     * @return $this
     */
    public function delAll($shoppingCarIds,$userId){

       return self::where('id','IN',$shoppingCarIds)
            ->where('userId','=',$userId)
            ->update(['status'=>self::STATUS_DELETE]);
    }
}